import React from "react";
import {inject, observer} from 'mobx-react';
import SecondAuthCom from "../../../workflowpublic/components/second-auth/index";
import QYSAuthCom from "../../../workflowpublic/components/second-auth/QYSAuth";
import SecondAuthComStore from "../../../workflowpublic/stores/SecondAuthStore";
import * as authTypes from "../../../workflowpublic/constants/secondAuthType";
import Loading from "./AsyncLoading";

@inject("secondAuthStore")
@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {store: null};
    }

    componentDidMount() {
        this.setState({
            store: new SecondAuthComStore()
        });
    }

    //验证成功后的回调方法
    onAuthSuccess(result) {
        const {secondAuthStore, ctype = 0} = this.props;
        secondAuthStore.controlDialogVisible(authTypes.SECOND_AUTH_DIALOG, false);       //关闭窗口

        //契约锁认证或者保护打开的窗口
        if (ctype == 1) {
            window.historyRoute.go(-1);
        }

        const {callbackFunc, callbackParam} = secondAuthStore;
        const {secondAuthType, isProtectDialog} = result;

        if (isProtectDialog == 1) {
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
        } else if (isProtectDialog == 10) {
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        } else {
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        }

        //契约锁验证，保存印章信息
        if (secondAuthType == authTypes.QYS) {
            const {sealId: qysSealId, sealImg: qysSealImg, certDetails: qysCertDetails} = result;    //契约锁印章，印章id信息,certDetails：印章信息，json字符串
            secondAuthStore.setAuthResult({
                qysSealId,
                qysSealImg,
                qysCertDetails: JSON.stringify(qysCertDetails)
            });
        }

        if (callbackFunc && typeof callbackFunc == "function") {
            callbackFunc.call({}, callbackParam);    //调用提交流程的方法
        }
    }

    //验证失败后的回调方法
    onAuthFail() {
        //nothing to do
    }

    //关闭窗口
    onClose() {
        const {secondAuthStore, ctype = 0} = this.props;
        secondAuthStore.controlDialogVisible(authTypes.SECOND_AUTH_DIALOG, false);

        //契约锁认证或者保护打开的窗口
        if (ctype == 1) {
            window.historyRoute.go(-1);
        }

        secondAuthStore.resetStore();
    }

    render() {
        const {store} = this.state;
        const {secondAuthStore, ctype = 0} = this.props;
        const {protectType, isProtectDialog, authKey, sourcestr, dialogVisible, signUrl} = secondAuthStore;
        let {secondAuthType} = secondAuthStore;

        if (!!store) {
            if (isProtectDialog == 1) {
                secondAuthType = protectType;
            }

            store.setAuthData({authKey, sourcestr, secondAuthType, isProtectDialog, signUrl});

            if (ctype == 1) {
                return <QYSAuthCom
                    secondAuthStore={store}
                    onSuccess={this.onAuthSuccess.bind(this)}
                    onFail={this.onAuthFail.bind(this)}
                    onClose={this.onClose.bind(this)}/>
            } else {
                return (
                    <SecondAuthCom
                        visible={dialogVisible.get(authTypes.SECOND_AUTH_DIALOG)}
                        secondAuthStore={store}
                        onSuccess={this.onAuthSuccess.bind(this)}
                        onFail={this.onAuthFail.bind(this)}
                        onClose={this.onClose.bind(this)}/>
                );
            }
        } else {
            return <Loading/>;
        }
    }
}
